<template>
	<view class="message-card" @click.stop="handleAccept">
		<!-- 邀请内容区域 -->
		<view class="invite-content">
			<!-- 邀请文案 -->
			<view class="invite-text">
				<text class="invite-desc">邀请加入群聊</text>
				<text class="group-name">{{ `(${message.content.groupInfo.groupName})` }}</text>
			</view>

			<!-- 群信息 -->
			<view class="group-info">
				<!-- 群头像 -->
				<view class="group-pics">
					<view class="" v-for="(pic, index) in message.content.groupInfo.groupPicPath" :key="index">
						<view class="pic-item">
							<image :src="pic.tempFileURL" mode="aspectFill"></image>
						</view>
					</view>
				</view>

				<!-- 群名称和成员数量 -->
				<view class="group-detail">
					<view class="group-member-count">
						{{ '群介绍：' + message.content.groupInfo.signature }}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { error } from '../../uni_modules/uv-ui-tools/libs/function';
import AvatarFallback from '../AvatarFallback.vue';
const group = uniCloud.importObject('groupManager', { customUI: true });
import setToTarget from '../../utils/setTarget';
export default {
	name: 'GroupInviteMessageCard',
	components: {
		AvatarFallback
	},
	props: {
		// 群邀请消息对象
		message: {
			type: Object,
			required: true,
			default: () => ({})
		}
	},
	methods: {
		/**
		 * 处理接受邀请
		 */
		async handleAccept() {
			try {
				const res = await group.applyJoin(this.$store.state.msg.user.userId, {
					groupId: this.message.content.groupInfo.groupId,
					userLocation: this.$store.state.msg.location
				});
				console.log(res);
				if (res.code === 1||res.code===-2) {
					console.log(this.message.content.groupInfo.groupId,this.message.content.groupInfo.groupName);
					const queryParam = {};
					queryParam.id = this.message.content.groupInfo.groupId;
					queryParam.type = 'group';
					this.$store.commit('msg/SET_queryParam', queryParam);
					setToTarget('group', { groupId: this.message.content.groupInfo.groupId, groupName: this.message.content.groupInfo.groupName });
					uni.navigateTo({
						url: '/pages/messages/messages'
					});
				} else {
					uni.showToast({
						title: res.message,
						position: 'center'
					});
				}
			} catch (e) {
				uni.showToast(
					{
						title: e,
						icon: 'error'
					},
					2000
				);
			}
		},

		/**
		 * 处理拒绝邀请
		 */
		handleReject() {
			this.$emit('reject', this.message);
		}
	}
};
</script>

<style lang="scss" scoped>
/* 消息卡片整体样式 */
.message-card {
	display: flex;
	align-items: flex-start;
	padding: 16rpx 25rpx;
	background-color: #fff;
	border-radius: 16rpx;
	box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
	margin: 10rpx;
}

/* 发送者头像 */
.sender-avatar {
	width: 64rpx;
	height: 64rpx;
	border-radius: 50%;
	overflow: hidden;
	margin-right: 16rpx;
	flex-shrink: 0;

	image {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}
}

/* 邀请内容区域 */
.invite-content {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: center;
	.group-name {
		font-size: 28rpx;
		color: #333;
		font-weight: 600;
	}

	.group-member-count {
		font-size: 26rpx;
		color: #55aaff;
		margin-top: 4rpx;
	}
}

/* 邀请文案 */
.invite-text {
	font-size: 28rpx;
	color: #333;
	line-height: 1.4;

	.sender-name {
		font-weight: 500;
		color: #007aff;
	}

	.invite-desc {
		color: #666;
	}
}

/* 群信息 */
.group-info {
	display: flex;
	flex-direction: column;
	align-items: center;
	margin-top: 12rpx;
}

.group-pics {
	display: flex;
	gap: 12rpx; /* 图片之间的间距 */
	padding: 10rpx 0;
	/* 可选：超出一行时自动换行 */
	flex-wrap: wrap;
}

/* 图片容器：控制图片大小和样式 */
.pic-item {
	width: 80rpx;
	height: 80rpx;
	border-radius: 8rpx; /* 圆角 */
	overflow: hidden; /* 避免图片溢出圆角 */
}

/* 图片样式：填充容器 */
.pic-item image {
	width: 100%;
	height: 100%;
	object-fit: cover; /* 保持图片比例，填充容器（和 mode="aspectFill" 效果一致） */
}

/* 群名称和成员数量 */
.group-detail {
	display: flex;
	flex-direction: column;
}

/* 操作按钮区域 */
.action-buttons {
	display: flex;
	flex-direction: column;
	margin-left: 16rpx;

	.btn {
		width: 120rpx;
		height: 48rpx;
		line-height: 48rpx;
		border-radius: 24rpx;
		font-size: 24rpx;
		margin-bottom: 10rpx;
		padding: 0;
	}

	.btn-accept {
		background-color: #007aff;
		color: #fff;
	}

	.btn-reject {
		background-color: #f5f5f5;
		color: #666;
	}
}
</style>
